package com.wisdomparking.control.app;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wisdomparking.bean.Orders;
import com.wisdomparking.bean.Parking;
import com.wisdomparking.config.result.Message;
import com.wisdomparking.control.BaseController;
import com.wisdomparking.service.OrdersService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.UserService;
import com.wisdomparking.service.VehicleInfoService;
import com.wisdomparking.tools.ToolDateTime;
import com.wisdomparking.tools.ToolValid;

/**
 * 订单中心
 */
@RestController
@RequestMapping("/app/orderCenter")
public class OrderController extends BaseController{
	
	@Autowired OrdersService ordersService;
	@Autowired VehicleInfoService vehicleInfoService;
	@Autowired ParkingService parkingService;
	@Autowired UserService userService;
	
//	/**
//	 * 生成订单
//	 * 
//	 * @param plateNumber		车牌
//	 * @param identification		车场唯一标识
//	 * @param memberFlag			车场是否会员标记
//	 * @return
//	 */
//	@RequestMapping("/createOrder")
//	public Message createOrder(String plateNumber, String identification, Boolean memberFlag){
//		
//		boolean flag = false;
//		
//		if(ToolValid.isNotEmpty(plateNumber) && ToolValid.isNotEmpty(identification)){
//			VehicleInfo vehicleInfo = vehicleInfoService.getVehicleInfoByPlateNumber(plateNumber);
//			Parking parking = parkingService.getParkingByIdentification(identification);
//			
//			Date date = new Date();
//			Orders order = new Orders();
//			
//			if(ToolValid.isNotNull(vehicleInfo)){
//				order.setUserId(vehicleInfo.getUserId());
//			}
//			else{
//				// 系统不存在车辆数据
//				vehicleInfo = new VehicleInfo();
//				vehicleInfo.setPlateNumber(plateNumber);
//				vehicleInfo.setCreateTime(new Date());
//				
//				vehicleInfoService.addVehicleInfo(vehicleInfo, true);
//			}
//			order.setMemberFlag(memberFlag);
//			order.setOrderNumber(ToolString.generateSingleNumber("JHZL_"));
//			order.setParkingId(parking.getId());
//			order.setParkingAddress(parking.getAddress());
//			order.setParkingName(parking.getName());
//			order.setStartTime(date);
//			order.setIdentification(identification);
//			order.setPlateNumber(plateNumber);
//			order.setStatus(0);
//			order.setCreateTime(date);
//			
//			flag = ordersService.addOrders(order, true);
//			
//		}
//		
//		return new Message(flag);
//	}
	
	
	/**
	 * 获取用户订单
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping("/getOrderList")
	public List<Orders> getOrderList(Integer userId, HttpServletRequest request){
		
		int pageNo = 1;
		int pageSize = 5;
		
		if(null!=request.getParameter("pageNo")){
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		
		if(null!=request.getParameter("pageSize")){
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("userId", userId);
		
		map.put("start", (pageNo-1)*pageSize);
		map.put("limit", pageSize);
		
		return ordersService.getOrdersList(map);
	}
	
	
	/**
	 * 获取订单详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getOrderDetail")
	public Orders getOrderDetail(Integer id){
		
		return ordersService.getOrders(id);
	}
	
	
	/**
	 * 获取订单支付金额
	 * @param plateNumber
	 * @return
	 */
	@RequestMapping("/getPaymentAmount")
	public Message getPaymentAmount(String plateNumber){
		Map<String, Object> map = null;
		
		Map<String, Object> m = new HashMap<>();
		m.put("plateNumber", plateNumber);
		m.put("statusLess", 2);
		m.put("orderByField", "id");
		m.put("orderByFlag", "desc");
		
		List<Orders> list = ordersService.getOrdersList(m);
		if(null!=list && list.size()>0){
			Orders orders = list.get(0);
			if(ToolValid.isNotNull(orders)){
				Parking parking = parkingService.getParking(orders.getParkingId());
				
				map = new HashMap<>();
				map.put("orderNumber", orders.getOrderNumber());
				map.put("plateNumber", plateNumber);
				int paymentAmount = 0;
				int min = ToolDateTime.getDateMinuteSpace(orders.getStartTime(), new Date());
				paymentAmount = Integer.parseInt(parking.getChargingA()) * min / 60;
				
				map.put("paymentAmount", new DecimalFormat("#.00").format(paymentAmount));
				
			}
		}
		
		return new Message(map);
	}
	
	
	/**
	 * 结束计费
	 * 
	 * @param plateNumber
	 * @return
	 */
	@RequestMapping("/endCharging")
	public Message endCharging(String plateNumber){
		boolean flag = false;
		Map<String, Object> m = new HashMap<>();
		m.put("plateNumber", plateNumber);
		m.put("status", 0);
		m.put("orderByField", "id");
		m.put("orderByFlag", "desc");
		
		List<Orders> list = ordersService.getOrdersList(m);
		if (null!=list && list.size()>0) {
			Orders orders = list.get(0);
			if (ToolValid.isNotNull(orders)) {
				Parking parking = parkingService.getParking(orders.getParkingId());
				
				int paymentAmount = 0;
				int min = ToolDateTime.getDateMinuteSpace(orders.getStartTime(), new Date());
				paymentAmount = Integer.parseInt(parking.getChargingA()) * min / 60;
				orders.setAmount(String.valueOf(paymentAmount * 100));
				orders.setStatus(1);
				
				flag = ordersService.updateOrders(orders, true);
			}
		}
		return new Message(flag);
	}
	
	
}
